package com.java2207.group1.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.java2207.group1.bo.EquipmentsBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.common.R;
import com.java2207.group1.listener.WebEquipmentsListener;
import com.java2207.group1.pojo.Equipments;
import com.java2207.group1.service.EquipmentsService;
import com.java2207.group1.util.OssUtil;
import com.java2207.group1.vo.equipment.BarVo;
import com.java2207.group1.vo.equipments.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author lz
 * @Date 2022/7/26 16:46
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentsController {

    @Autowired
    private EquipmentsService equipmentsService;


    @Autowired
    private WebEquipmentsListener webEquipmentsListener;

    @RequestMapping("/equipmentList")
    public LayuiPageVo<Equipments> equipmentList(@RequestParam Map<String, Object> params){
        return equipmentsService.equipmentList(params);
    }

    @RequestMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        if(file!=null){
            //得到文件名
            String originalFilename = file.getOriginalFilename();

            System.out.println(originalFilename);
            InputStream inputStream = file.getInputStream();

            String url = OssUtil.uploadFile(inputStream, originalFilename);

            //把文件 url 写给前端

            return R.ok().data("url",url);
        }
        return R.error();
    }

    @RequestMapping("/updateEquipment")
    public R updateEquipment(Equipments equipments){

        int result = equipmentsService.updateEquipment(equipments);
        if(result == 1){
            return R.ok();
        }
        return R.error();
    }


    @RequestMapping("/addEquipment")
    public R addEquipment(Equipments equipments){
        int result = equipmentsService.addEquipment(equipments);
        if(result == 1){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("/deleteEquipment")
    public R deleteEquipment(int id){
        int result = equipmentsService.deleteEquipment(id);
        if(result == 1){
            return R.ok();
        }
        return R.error();
    }


    @RequestMapping("/echartBar")
    public R echartBar(){
        List<BarVo> list = equipmentsService.selectEchart();
        System.out.println(list);
        return R.ok().data("equipmentBar", list);
    }

    @RequestMapping("/echartsPie")
    public R echartsPie(){
        List<PieVo> list = equipmentsService.selectPie();
        return R.ok().data("equipmentPie", list);
    }

    @RequestMapping("/export")
    public void export(Equipments equipments, HttpServletResponse response) throws IOException {
        //下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("库存信息导出", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, EquipmentsBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        List<EquipmentsBo> orders =   equipmentsService.equipmentsExcelData(equipments);

        sheet.doWrite(orders);
    }

    @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        if(file!=null){
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), EquipmentsBo.class, webEquipmentsListener);

            ExcelReaderSheetBuilder sheet = read.sheet();

            sheet.doRead();

        }
        return R.ok();
    }
}
